﻿--[[
   @brief    wonderful panel control
   @author   hqzheng
   @version  1.0
   @date     2015-06-20
--]]

game_mgr_control = {
    init = function()        
        ierd_tgp.tpf_ui.Require('game_mgr\\model.lua');
        ierd_tgp.tpf_ui.Require('game_mgr\\view.lua');
        base_utility.log("[game_mgr_control] init success.");
        local tpfui = base_utility.get_component_adapter(
                        "ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        game_mgr_view.init();

        comm_center.reg_service("Svr_ListenGKUpdateState", "game_mgr_control.handle_listen_state");
        comm_center.reg_service("", "game_mgr_control");
    end;

    -- 统一处理JS的回调
    handle_js_callback = function(event_id, json_param)
        local param = base_utility.json_to_table(json_param);
        local func_name = param.func_name;
        local json = param.json;
        ierd_tgp.tpf_ui.Log("[game_mgr]handle_js_callback.func_name="..func_name);
        if func_name == "m_add_wandering_game" then
            game_mgr_control.handle_add_wandering_game(true, json);
        elseif func_name == "m_remove_wandering_game" then
            game_mgr_control.handle_add_wandering_game(false, json);
        elseif func_name == "jc_get_installed_games" then
            game_mgr_control.handle_get_installed_games(json);
        end
    end;

    -- 添加游戏到我的游戏列表中
    handle_add_wandering_game = function(added, json)
        base_utility.log(
            string.format("[game_library_control][handle_add_wandering_game]json:%s", json));
        local param = base_utility.json_to_table(json);
        if nil ~= param and nil ~= param.data then
            local gl_adapter =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            if gl_adapter ~= nil then
                -- 先判断游戏状态，如果正在下载，则取消下载：
                local cancel_download = false;
                if not added then
                    local state = banner_model.get_game_state(tonumber(param.data.game_id));
                    if state ~= nil and state.iState ~= nil then
                       if state.iState == ierd_tgp.game_library.GAME_STATE_DOWNLOADING or
                           state.iState == ierd_tgp.game_library.GAME_STATE_DOWNLOAD_PAUSED or
                           state.iState == ierd_tgp.game_library.GAME_STATE_DOWNLOADED then
                           gl_adapter:cancel_download_game(tonumber(param.data.game_id));
                           cancel_download = true;
                       end
                    end;
                end
                if not cancel_download then
                    gl_adapter:add_or_remove_game(tonumber(param.data.game_id), added);
                end
            end
            local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
            if param.callback_func ~= nil and param.callback_func ~= "" then
                local rsp_json = base_utility.table_to_json(rsp_table);
                web_utility.lua_call_js_ex(param.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
            end
        end
    end;
    -- 给页面返回本地已经安装的游戏
    handle_get_installed_games = function(json)
        local param = base_utility.json_to_table(json);
        if nil ~= param then
            local installed_game_list = ierd_tgp.game_library.Game_list:new_local();
            local gl_adapter =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            if gl_adapter ~= nil then
                gl_adapter:get_installed_games(installed_game_list);
            end
            local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
            rsp_table.data = {};
            rsp_table.data.game_id_list = {};
            for i = 0, installed_game_list.count - 1 do
                local game_id = installed_game_list.game_id_list[i];
                if not game_utility.is_sub_in_attach_rel(game_id) then
                    table.insert(rsp_table.data.game_id_list, game_id);
                end
            end
            if param.callback_func ~= nil and param.callback_func ~= "" then
                local rsp_json = base_utility.table_to_json(rsp_table);
                web_utility.lua_call_js_ex(param.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
            end
        end
    end;
    on_get_game_keeper_call = function(event_id, json)
        base_utility.log(string.format("[on_get_game_keeper_call]json:%s", json));

        local param = base_utility.json_to_table(json);
        if nil ~= param and
           nil ~= param.callback_func and 
           nil ~= param.data and 
           nil ~= param.data.req_type then

            if param.data.req_type == "gk_get_local_game_list" then
                game_mgr_control.on_gk_get_local_game_list(param);
            elseif param.data.req_type == "gk_open_game_directory" then
                game_mgr_control.on_gk_open_game_directory(param);
            elseif param.data.req_type == "gk_uninstall_game" then
                game_mgr_control.on_gk_uninstall_game(param);
            elseif param.data.req_type == "gk_fix_game" then
                game_mgr_control.on_gk_fix_game(param);
            elseif param.data.req_type == "gk_get_game_helper_info" then
                game_mgr_control.on_gk_get_game_helper_info(param);
            elseif param.data.req_type == "gk_get_local_record_mobile_games" then
                game_mgr_control.on_gk_get_local_record_mobile_games(param);
            elseif param.data.req_type == "gk_install_game" then
                game_mgr_control.on_gk_install_game(param);
            end;
        end;
     end;

    on_gk_open_game_directory = function(req_table)
        if nil ~= req_table.data.game_id then
            local game_id = tonumber(req_table.data.game_id);
            local game_lib =
               base_utility.get_component_adapter("IGame_library",
                      "ierd_tgp.game_library.Get_game_library_adapter();");
            if game_lib ~= nil then
                game_lib:open_game_path(game_id);
            end;
        end;
    end;

    on_service_uninstall_game = function(data)
        base_utility.log("[game_mgr_control] on_service_uninstall_game.");
        req_table = { data = data};
        game_mgr_control.on_gk_uninstall_game(req_table);
    end;
    on_gk_uninstall_game = function(req_table)
        if nil ~= req_table.data.game_id then
            local game_id = tonumber(req_table.data.game_id);
            local data_out = comm_center.call_service("i_service_allow_continue_operation_game",
                            {game_id = game_id});
            if data_out ~= nil and not data_out.bAllowContinue then
                base_utility.log("[game_mgr_control][on_gk_uninstall_game]not allow." ..
                    base_utility.table_to_json(data_out));
                return
            end

            local state = banner_model.get_game_state(game_id);
            if state == nil or state.iState == nil then
                return;
            end;
            if state.iState == ierd_tgp.game_library.GAME_STATE_UPDATING then
                base_utility.log("game is updating");
                return;
            end;

            local game_lib =
              base_utility.get_component_adapter("IGame_library",
                  "ierd_tgp.game_library.Get_game_library_adapter();");
            if game_lib ~= nil then
                game_lib:uninstall_game(game_id);
            end;
        end;
    end;

    on_gk_fix_game = function(req_table)
        if nil ~= req_table.data.game_id then
            local game_id = tonumber(req_table.data.game_id);
            local params = {};
            params.id = game_id;
            params.pathname = "";
            local str = base_utility.table_to_json(params);
            if game_id == 2 then
              event_center.send_event(tool_box_event.OPEN_CF_REPAIR_PAGE, str); 
            elseif game_id == 26 then
              event_center.send_event(tool_box_event.OPEN_LOL_REPAIR_PAGE, str);
            elseif game_id == GAME_ID.GAME_ID_DNF then
              event_center.send_event(tool_box_event.OPEN_DNF_REPAIR_PAGE, str);
            elseif game_id > 0 then
              event_center.send_event(tool_box_event.OPEN_REPAIR_GAME_PAGE, str); 
            end;
        end;
    end;

    on_gk_get_local_game_list = function(req_table)
        local rsp_table = web_utility.web_rsp_comm_data(req_table, 0, "");
        
        local  game_lib =
                    base_utility.get_component_adapter("IGame_library",
                        "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib ~= nil then
            local game_list_temp = ierd_tgp.game_library.GameMgrInfoList:new();
            game_lib:get_game_mgr_info(game_list_temp);
            local game_list_table = nil;
            game_list_table = {count = game_list_temp.count, game_list = {}};
            local itemCount = game_list_temp.count;
            while (itemCount > 0) 
               do table.insert(game_list_table.game_list,{
                    id = game_list_temp.game_mgr_info[itemCount - 1].id,
                    name = game_list_temp.game_mgr_info[itemCount - 1].name,
                    is_hidden = game_list_temp.game_mgr_info[itemCount - 1].is_hidden,
                    icon_url = game_list_temp.game_mgr_info[itemCount - 1].icon_url,
                    type = game_list_temp.game_mgr_info[itemCount - 1].type,
                    last_played_time = game_list_temp.game_mgr_info[itemCount - 1].last_played_time,
                    support_fix = game_list_temp.game_mgr_info[itemCount - 1].support_fix
                    });
                    itemCount = itemCount - 1;
            end;

            if game_list_table ~= nil then
                rsp_table.data = game_list_table;
                local rsp_json = base_utility.table_to_json(rsp_table);
                web_utility.lua_call_js_ex(req_table.callback_func, rsp_json, CID_MAINFRAME_QBWEB);  
            end;   
            game_list_temp:delete();
        end;
     end;

    on_gk_get_game_helper_info = function(req_table)
        local rsp_table = web_utility.web_rsp_comm_data(req_table, 0, "");
        local info = {};
        for k,v in pairs(banner_model.module_info) do
            if nil ~= k and nil ~= tonumber(k) and 0 ~= tonumber(k) and 
               nil ~= v and nil ~= v.module_url then
                table.insert(info, {
                    game_id = tonumber(k),
                    module_url = v.module_url
                });
            end;
        end;
        rsp_table.data = info;
        local rsp_json = base_utility.table_to_json(rsp_table);
        web_utility.lua_call_js_ex(req_table.callback_func, rsp_json, CID_MAINFRAME_QBWEB);  
    end;

    on_gk_get_local_record_mobile_games = function(req_table)
        if nil ~= req_table then
            local mobile_game_list = ierd_tgp.game_library.Game_list:new_local();
            local gl_adapter =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            if gl_adapter ~= nil then
            end
            local rsp_table = web_utility.web_rsp_comm_data(req_table, 0, "");
            rsp_table.data = {};
            rsp_table.data.game_id_list = {};
            for i = 0, mobile_game_list.count - 1 do
                table.insert(rsp_table.data.game_id_list, mobile_game_list.game_id_list[i]);
            end
            if req_table.callback_func ~= nil and req_table.callback_func ~= "" then
                local rsp_json = base_utility.table_to_json(rsp_table);
                web_utility.lua_call_js_ex(req_table.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
            end
        end
    end;

    on_gk_install_game = function(req_table)
        if nil ~= req_table.data.game_id then
            local game_id = tonumber(req_table.data.game_id);
            local data_out = comm_center.call_service("i_service_allow_continue_operation_game",
                            {game_id = game_id});
            if data_out ~= nil and not data_out.bAllowContinue then
                return
            end

            local game_lib =
              base_utility.get_component_adapter("IGame_library",
                  "ierd_tgp.game_library.Get_game_library_adapter();");
            if game_lib ~= nil then
              game_lib:install_game(game_id);
            end;
        end;
    end;

    handle_listen_state = function(zParam)
        base_utility.log("handle_listen_state: " .. base_utility.table_to_json(zParam));
        if (nil == zParam) or (nil == zParam["opened"]) then
            return;
        end;
        if zParam["opened"] == 1 then
            game_mgr_model.web_listen_state_opened = true;
        else
            game_mgr_model.web_listen_state_opened = false;
        end;
    end;

    gk_update_game_state = function(event_id, json_str)
        if json_str == nil then
            return;
        end;
        base_utility.log("gk_update_game_state:" .. json_str);

        if game_mgr_model == nil or game_mgr_model.web_listen_state_opened == false then
            return;
        end;

        local params = base_utility.json_to_table(json_str);
        if nil == params or nil == params.game_id or 0 == params.game_id then
            return;
        end;

        local rsp = {};
        rsp.game_id = params.game_id;
        local state = banner_model.get_game_state(params.game_id);
        if state == nil or state.iState == nil then
            return;
        end;

        rsp.game_state = state.iState;

        local rsp_json = base_utility.table_to_json(rsp);
        if rsp_json == nil then
            return;
        end;

        if nil ~= main_web_control then
            main_web_control.lua_call_JS("gk_update_game_state", rsp_json);
        end;
    end;

    on_service_open_game_directory = function(data)
        if data == nil or data.game_id == nil then
            base_utility.log("on_service_open_game_directory: param data is nil");
            return;
        end
        local param = {data=data};
        game_mgr_control.on_gk_open_game_directory(param);
    end;
}
